package com.showcai.controller.product;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.ProductEntity;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.service.ProductEntityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zpc on 2017/3/31.
 */
@RequestMapping(value = "/productEntity")
@Controller
public class ProductEntityController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ProductEntityService productEntityCertificateService;


    /**
     * 获取产品列表
     * @param productClassifyId
     * @return
     */
    @RequestMapping(value = "/getProductEntityList/{productClassifyId}",method = RequestMethod.POST)
    @ResponseBody
    public String getProductEntityList(@PathVariable("productClassifyId")Byte productClassifyId,Integer page,Integer pageSize) {

        ResultObj resultObj = new ResultObj();

        try {
            Map<String,String> map=new HashMap<>();
            map.put("productClassifyId",String.valueOf(productClassifyId));

            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }
            map.put("page",String.valueOf(page));
            map.put("pageSize",String.valueOf(pageSize));
           PageInfo pageInfo = productEntityCertificateService.getPageInfoProductEntityList(map);
            resultObj.setData(pageInfo);
        } catch (MyRunTimeExcption e) {
            logger.warn(e.getMessage());
            resultObj.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.warn(e.getMessage());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj.toJson();
    }

    /**
     * 获取产品详情
     * @param productEntityId
     * @return
     */
    @RequestMapping(value = "/getProductEntity/{productEntityId}",method = RequestMethod.POST)
    @ResponseBody
    public String getProductEntityList(@PathVariable("productEntityId") Long productEntityId) {
        ResultObj resultObj = new ResultObj();
        try {
         ProductEntity productEntity = productEntityCertificateService.getProductEntityById(productEntityId);
         logger.info("根据产品实体ID拿到的产品实体！————————————————"+productEntity);
         resultObj.setData(productEntity);
        } catch (MyRunTimeExcption e) {
            logger.warn(e.getMessage());
            resultObj.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.warn(e.getMessage());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj.toJson();
    }




}
